﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LcfContols
{

    public class DataGridViewPanelRow : DataGridViewRow
    {
        #region 变量

        /// <summary>
        /// 之前的DataGridView;
        /// </summary>
        private DataGridView _oldDataGridView;
        /// <summary>
        /// 垂直滚动条
        /// </summary>
        private ScrollBar vscroll;
        #endregion

        #region 属性
        public Control Control
        {
            get
            {
                return HeaderCell.Value as Control;
            }
            set
            {
                HeaderCell.Value = value;
                Height = value.Height;
                SetControlParent();
            }
        }
        
        #endregion

        protected override void OnDataGridViewChanged() //当所属的DataGridView变化后绑定的事件跟随变化
        {
            if (_oldDataGridView != null)
            {
                _oldDataGridView.ParentChanged -= DataGridView_ParentChanged;
                _oldDataGridView.Scroll -= DataGridView_Scroll;
            }
            _oldDataGridView = DataGridView;
            if (DataGridView != null)
            {
                DataGridView.ParentChanged += DataGridView_ParentChanged;
                DataGridView.Scroll += DataGridView_Scroll;
            }
            base.OnDataGridViewChanged();
        }

        private void DataGridView_Scroll(object sender, ScrollEventArgs e)
        {
            SetControlParent();
        }

        #region 设置控件的父级
        private void DataGridView_ParentChanged(object sender, EventArgs e)
        {
            //控件父类改变，当前控件也需要改变
            if (Control != null)
            {
                if (Control.Parent != null && Control.Parent.Controls.Contains(Control))
                {
                    Control.Parent.Controls.Remove(Control);
                }
                SetControlParent();
            }
        }

        public override bool Visible
        {
            get
            {
                return base.Visible;
            }
            set
            {
                base.Visible = value;
                if(Control != null)
                    Control.Visible = false;
            }
        }

        /// <summary>
        /// 设置垂直滚动条，用来调整位置放置被遮挡
        /// </summary>
        /// <param name="bar"></param>
        internal void SetVScrollBar(ScrollBar bar)
        {
            vscroll = bar;
        }

        private void SetControlParent()
        {
            if (DataGridView != null)
            {
                if (!DataGridView.Controls.Contains(Control)){
                    DataGridView.Controls.Add(Control);
                    if (vscroll != null)
                        vscroll.BringToFront();
                }     
                int index = DataGridView.Rows.IndexOf(this);
                if (index < 0 || Control == null){
                    return;
                }
                Control.Width = DataGridView.Columns.GetColumnsWidth(DataGridViewElementStates.None) + DataGridView.RowHeadersWidth;
                Control.Height = Height;
                Rectangle rect = DataGridView.GetRowDisplayRectangle(index, false);
                if (rect.Y > 0)
                {
                    Control.Visible = true;
                    Control.Location = new Point(0, rect.Y);
                }
                else
                {
                    Control.Visible = false;
                }
            }
        }

        #endregion
        protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, int rowIndex, DataGridViewElementStates rowState, bool isFirstDisplayedRow, bool isLastVisibleRow)
        {
            if (Control != null)
            {
                SetControlParent();
            }
            else{
                graphics.FillRectangle(SystemBrushes.Control, rowBounds);
            }
            
        }
    }
}
